﻿using System;
using System.Collections.Generic;
using System.Text;
using Components.Enum;
using Model;
using ServiceHelper.Util;

namespace ServiceHelper.Doc
{
    public class BackAdjustManager : DocumentManager
    {
        public override Sto8a00 CreateDefaultModel()
        {
            var model = new Sto8a00();
            model.Doctype = DocumentType.PurchaseReturn;
            return model;
        }

        public void ParseRow(Sto8a01 model)
        {
            if (model != null)
            {
                Doc.Rows.Add(model);
            }
        }

        protected override void AfterSubmit()
        {
            var exec = Exec.Create();

            //累加行的调整金额和调整数量
            foreach (var row in Doc.Rows)
            {
                if (string.IsNullOrEmpty(row.Dtlrefno1))
                    continue;

                exec.AddSql(Ord4a01Manager.Entity.DoAdjust(row.Dtlrefno1, row.Actqty1 - row.Preqty1, row.Dtltotalamt));
            }

            //更新到Ord3a00中
            exec.AddSql(Ord4a00Manager.Entity.DoAdjust(Doc.Refid, Doc.Totalqty, Doc.Totalamt));

            exec.Do();
        }
    }
}
